<!-- 设备维修申请 -->
<template>
  <div class="app-container">
    <div class="cardBox">
      <leftTree ref="leftTree" :data="treeData" node-key="id" :default-props="defaultProps" search-key="typeName" width="200px" :default-expand-keys="defaultExpandKeys" :defalut-expand-all="true" @handleNodeClickAll="handleNodeClick" />
      <el-col id="right-content" class="right-content" :offset="0" :span="18">
        <div class="box-title">
          <el-breadcrumb separator="/" style="line-height:50px;">
            <el-breadcrumb-item v-for="(item,index) in breadcrumb" :key="index">{{ item }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="search-and-button noLeftInput">
          <div class="btnList">
            <el-button type="primary" icon="el-icon-plus" @click="showDialog">新建</el-button>
            <ImportFile url="/basic/equip/import" @getTableInfo="getTableInfo" />
            <el-button type icon="el-icon-refresh-left" @click="$router.push('/reload')">刷新</el-button>
            <el-button type icon="el-icon-more">更多</el-button>
            <el-button type="primary" plain :icon="!show ? 'el-icon-arrow-down' : 'el-icon-arrow-up'" @click="show = !show">查询条件</el-button>
          </div>
        </div>
        <transition name="el-zoom-in-top">
          <div v-show="show" class="more-search">
            <el-form :model="search" label-width="80px">
              <el-row>
                <el-col :xs="24" :xl="8" :md="8">
                  <el-form-item label="状态" prop="status">
                    <el-select v-model="search.status" @change="getTableInfo()" style="width: 100%">
                      <el-option label="全部" value></el-option>
                      <el-option label="新建" :value="1"></el-option>
                      <el-option label="已下达" :value="2"></el-option>
                      <el-option label="维修中" :value="3"></el-option>
                      <el-option label="已完成" :value="4"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :xl="8" :md="8">
                  <el-form-item label="维修方式" prop="repairMethod">
                    <el-select v-model="search.repairMethod" @change="getTableInfo()" style="width: 100%">
                      <el-option label="全部" value></el-option>
                      <el-option label="待确认" :value="0"></el-option>
                      <el-option label="内修" :value="1"></el-option>
                      <el-option label="外修" :value="2"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :xl="8" :md="8">
                  <el-form-item label="故障日期" prop="beginDate">
                    <el-date-picker v-model="search.time" style="width: 100%" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择日期时间" @change="getTableInfo()" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </transition>
        <el-row class="table">
          <el-table ref="tableData" v-loading="loading" :data="tableData" stripe border @selection-change="handleSelectionChange">
            <el-table-column type="selection" align="center"></el-table-column>
            <el-table-column align="center" type="index" label="索引" width="60px" />
            <el-table-column v-for="item in fieldList" :key="item.id" :align="item.align || 'left'" :prop="item.code.split(':')[1]" :label="item.val" :min-width="item.width + 'px'" show-overflow-tooltip>
              <template #default="{ row }">
                <span v-if="item.code.split(':')[1] === 'applyNo'" class="tabNoStyle" @click="applyNoDetails(row)">{{ row.applyNo}}</span>
                <span v-else-if="item.code.split(':')[1] === 'repairMethod'">{{ row.repairMethod == 0 ? '待确认' : row.repairMethod == 1 ? '内修':'外修' }}</span>
                <span v-else-if="item.code.split(':')[1] === 'isSolve'">{{ row.isSolve == '1' ? '是':'否' }}</span>
                <span v-else-if="item.code.split(':')[1] === 'status'">
                  <span v-if="row.status == 1">新建</span>
                  <span v-else-if="row.status == 2">已下达</span>
                  <span v-else-if="row.status == 3">维修中</span>
                  <span v-else>已完成</span>
                </span>
                <span v-else>{{ row[item.code.split(':')[1]] }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" align="center" width="100px">
              <template #default="{ row }">
                <el-button-group>
                  <el-button v-if="row.status == 1" type="text" @click="showDialog(row)">
                    <i class="iconfont icon-search"></i>
                  </el-button>
                </el-button-group>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
        <el-row>
          <div class="table-btn-box">
            <el-button @click="
                () => {
                  tableData.forEach(row => {
                    $refs['tableData'].toggleRowSelection(row, true)
                  })
                }
              ">选择全部</el-button>
            <el-button @click="
                () => {
                  tableData.forEach(row => {
                    $refs['tableData'].toggleRowSelection(row)
                  })
                }
              ">反向选择</el-button>
            <el-button>
              更多菜单
              <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
          </div>
          <pagination ref="Pagination" :total="total" :current.sync="search.current" :size.sync="search.size" @pagination="getTableInfo" />
        </el-row>
      </el-col>
    </div>

    <!-- 新增编辑 -->
    <addAndModify ref="addAndModify" :fault-classify-list="faultClassifyList" :user-all-list="userAllList" :eq-all-list="eqAllList" @getTableInfo="getTableInfo" />
    <detail-info ref="detailInfo" :user-all-list="userAllList" @getTableInfo="getTableInfo" />
  </div>
</template>
<script>
import {
  getFaultClassifyListApi,
  getEqListApi,
  getUserListApi
} from '@/api/public'
import ImportFile from '@/components/ImportFile'
import Pagination from '@/components/Pagination'
import addAndModify from './components/index'
import detailInfo from './components/detailInfo'
import leftTree from '@/components/LeftTree'
import { mapGetters } from 'vuex'
export default {
  components: {
    leftTree,
    Pagination,
    ImportFile,
    addAndModify,
    detailInfo
  },
  data() {
    return {
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'typeName'
      },
      defaultExpandKeys: ['-1'],
      breadcrumb: ['设备类型目录'],
      loading: false,
      tableData: [],
      multipleSelection: [],
      total: 0,
      userAllList: [],
      search: {
        current: 1,
        size: 20,
        objNo: '',
        keyword: '',
        beginDate: '',
        endDate: '',
        time: [],
        treeStr: '-1',
        repairMethod: '',
        status: ''
      },
      show: false,
      faultClassifyList: [],
      eqAllList: []
    }
  },
  computed: {
    ...mapGetters(['compInfo'])
  },
  created() {},
  async mounted() {
    await this.getTableInfo()
    await this.getEqListAll()
    await this.getUserAll()
    await this.getAllClassify()
    await this.getFaultClassify()
  },
  methods: {
    async getFaultClassify() {
      const res = await this.$axios.get('/equip/repair/faultClassify/getList')
      if (res?.data.code === 200) {
        this.faultClassifyList = res.data.data
      }
    },
    /**
     * @description: 查询所有设备类型
     * @return {*}
     */
    async getAllClassify() {
      const res = await this.$axios.get('/equip/info/allClassify')
      if (res?.data.code === 200) {
        this.treeData = [
          {
            id: '-1',
            typeName: '设备类型目录',
            children: this.arrConvert(res.data.data)
          }
        ]
      }
    },
    arrConvert(arr) {
      let arr2 = []
      for (let key in arr) {
        let obj = {
          typeName: arr[key],
          id: arr[key]
        }
        arr2.push(obj)
      }
      return arr2
    },
    /* 获取所有设备 */
    async getEqListAll() {
      const res = await getEqListApi()
      if (res?.data.code === 200) {
        this.eqAllList = res.data.data
      }
    },
    /* 获取所有人员 */
    async getUserAll() {
      const res = await getUserListApi()
      if (res?.data.code === 200) {
        this.userAllList = res.data.data
      }
    },
    timeDivide() {
      const { time } = this.search
      if (time && time.length) {
        this.search.beginDate = time[0]
        this.search.endDate = time[1]
      } else {
        this.search.beginDate = ''
        this.search.endDate = ''
      }
      const search = {
        ...this.search
      }
      delete search.time
      return search
    },
    async getTableInfo(page = 1) {
      let search = this.timeDivide()
      search.current = page
      this.loading = true
      const res = await this.$axios.post('/equip/repair/order/list', search)
      if (res?.data.code === 200) {
        this.tableData = res.data.data.list
        this.total = res.data.data.total
        setTimeout(() => {
          this.loading = false
        }, 100)
      } else {
        this.loading = false
      }
    },
    /**
     * @description: 新增编辑
     * @return {*}
     */
    showDialog(row) {
      this.$refs['addAndModify'].showDialog(row)
    },
    /**
     * @description: 树节点点击
     * @param {*} data
     * @param {*} node
     * @return {*}
     */
    handleNodeClick({ data, node }) {
      this.breadcrumb = []
      this.returnNodeLabel(node.parent)
      this.breadcrumb.push(data.typeName)
      this.search.treeStr =
        data.typeName === '设备类型目录' ? '-1' : data.typeName
      this.getTableInfo()
    },
    /**
     * @description: 递归获取名称
     * @param {*} node
     * @return {*}
     */
    returnNodeLabel(node) {
      if (node.parent) {
        this.breadcrumb.unshift(node.data.typeName)
        this.returnNodeLabel(node.parent)
      }
    },
    applyNoDetails(row) {
      this.$refs['detailInfo'].getInfo(row)
    },
    /**
     * @description: 选中项
     * @param {*} val
     * @return {*}
     */
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
